<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/_base/lang", "dojo/request", "dojo/topic", "dojo/json" ],
            function(array, xhr, lang, request, topic, JSON) {
                var deletItemsCount = 0;
                sectionRestForm = function(formId) {
                    dijit.byId(formId).reset();
                    if (formId.indexOf('UFD') != -1) {
                        var rowid = jQuery("#sectionGird").jqGrid('getGridParam', 'selrow');
                        var section = jQuery("#sectionGird").jqGrid('getRowData', rowid);
                        dijit.byId('sectionUFD').setValues(section);
                    }
                };
                topic.subscribe("section/insertOrUpdate", sectionRestForm);
                var syncDB = function(url, jsonData) {
                    request.post(url, {
                        data : jsonData,
                        headers : {
                            'Content-Type' : "application/json; charset=utf-8"
                        },
                        timeout : 30000
                    }).then(function(response) {
                        topic.publish("notifyMsgTopic", {
                            message : response,
                            type : "message",
                            duration : 1000
                        });
                        var currentPage = jQuery('#sectionGird').jqGrid('getGridParam', 'page');
                        var pageSize = jQuery('#sectionGird').jqGrid('getGridParam', 'rowNum');
                        var totalCount = jQuery('#sectionGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                        var totalPages = Math.ceil(totalCount / pageSize);
                        jQuery("#sectionGird").jqGrid('setGridParam', {
                            page : currentPage > totalPages ? totalPages : currentPage
                        }).trigger("reloadGrid");
                    }, function(error) {
                        alert(error);
                    });
                };
                sectionSubmitForm = function(formId, type) {
                    var url = "section/insert";
                    if (type == 'u') {
                        url = "section/update";
                    }
                    var form = dijit.byId(formId);
                    if (form.isValid()) {
                        var formData = form.getValues();
                        lang.mixin(formData, {username : username});
                        var json = JSON.stringify(formData);
                        syncDB(url, json);
                        form.hide();
                        topic.publish("section/insertOrUpdate", formId);
                    } else {
                        form.validate();
                    }
                };
                sectionDelete = function() {
                    var ids = jQuery("#sectionGird").jqGrid('getGridParam', 'selarrrow') || [];
                    deletItemsCount = ids.length;
                    if (ids.length > 0) {
                        if (confirm('确认删除所选定的项？')) {
                            var section = {};
                            section.id = '';
                            array.forEach(ids, function(entry, i) {
                                section.id += entry + ",";
                            });
                            syncDB("section/delete", JSON.stringify(section));
                        }
                    } else {
                        alert('请选择至少一条数据!');
                    }
                };
                sectionQuery = function(formId) {
                    var section = dijit.byId(formId).getValues();
                    jQuery("#sectionGird").jqGrid('setGridParam', {
                        postData : section,
                        mtype : "post"
                    }).trigger('reloadGrid');
                };
            });
</script>
<div data-dojo-type="dijit/Dialog" id="sectionIFD" title="新增">
  <div class="dijitDialogPaneContentArea">
    <table>
      <tr>
        <td><label for="sectionname">栏目名称: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="sectionname"></td>
      </tr>
      <tr>
        <td><label for="sectiondesc">栏目描述: </label></td>
        <td><input data-dojo-type="dijit/form/SimpleTextarea"  type="text" rows=3 cols=18  name="sectiondesc"></td>
      </tr>
      <tr>
        <td><label for="createtime">创建时间: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="createtime" onClick="WdatePicker()" ></td>
      </tr>
      <tr>
        <td><label for="status">是否激活: </label></td>
        <td>
            <select data-dojo-type="dijit/form/Select" name="status" style="width: 100%">
              <option value="1">激活</option>
              <option value="0">禁用</option>
            </select>
        </td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="sectionSubmitForm('sectionIFD','i')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="sectionRestForm('sectionIFD');">重置</button>
  </div>
</div>
<div data-dojo-type="dijit/Dialog" id="sectionUFD" title="更新">
  <div class="dijitDialogPaneContentArea">
    <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, readOnly: true" type="text"
      name="sectionid" style="display: none;">
    <table>
      <tr>
        <td><label for="sectionname">栏目名称: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="sectionname"></td>
      </tr>
      <tr>
        <td><label for="sectiondesc">栏目描述: </label></td>
        <td><input data-dojo-type="dijit/form/SimpleTextarea"  type="text" rows=3 cols=18  name="sectiondesc"></td>
      </tr>
      <tr>
        <td><label for="createtime">创建时间: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="createtime" onClick="WdatePicker()" ></td>
      </tr>
      <tr>
        <td><label for="status">是否激活: </label></td>
        <td>
            <select data-dojo-type="dijit/form/Select" name="status" style="width: 100%">
              <option value="">&nbsp;</option>
              <option value="0">禁用</option>
              <option value="1">激活</option>
            </select>
        </td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="sectionSubmitForm('sectionUFD','u')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="sectionRestForm('sectionUFD');">重置</button>
  </div>
</div>
<fieldset>
  <legend>&nbsp;&nbsp;信息选项&nbsp;&nbsp;</legend>
  <form id="querySectionForm" data-dojo-type="dijit/form/Form" method="post">
    <div style="margin-left: 5px;">
      <table>
        <tr>
          <td width="8%"><label for="sectionname">栏目名称: </label></td>
          <td width="16%"><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="sectionname"></td>
          <td width="8%"><label for="createtime">创建时间: </label></td>
          <td width="16%">
            <input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="createtime" onClick="WdatePicker()" style="width: 40%">—<input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="exptime" onClick="WdatePicker()" style="width: 40%">
           </td>
          <td width="8%"><label for="status">是否激活: </label></td>
          <td width="16%">
	        <select data-dojo-type="dijit/form/Select" name="status" style="width: 86%">
	          <option value="">全部</option>
	          <option value="0">禁用</option>
	          <option value="1">激活</option>
	        </select>
          </td>
        </tr>
      </table>
    </div>
  </form>
  <div style="padding: 5px;">
    <button id="sectionInsertBtn" data-dojo-type="dijit/form/Button" type="submit" label="新增">
      <script type="dojo/method" event="onClick" args="evt">
        dijit.byId('sectionIFD').show();
      </script>
    </button>
    <button id="sectionDeleteBtn" data-dojo-type="dijit/form/Button" type="button" label="删除" onclick="sectionDelete();"></button>
    <button id="sectionUpdateBtn" data-dojo-type="dijit/form/Button" type="button" label="更新">
      <script type="dojo/method" event="onClick" args="evt">
        var ids = jQuery("#sectionGird").jqGrid('getGridParam', 'selarrrow') || [];
        if (ids.length != 1) {
          alert('每次只能修改一条数据!');
        }else{
          var section = jQuery("#sectionGird").jqGrid('getRowData', ids[0]);
          dijit.byId('sectionUFD').setValues(section);
          dijit.byId('sectionUFD').show();
        }
      </script>
    </button>
    <button id="sectionQueryBtn" data-dojo-type="dijit/form/Button" type="button" label="查询"
      onclick="sectionQuery('querySectionForm');"></button>
  </div>
</fieldset>
<div id="gird">
  <table id="sectionGird"></table>
  <div id="sectionToolbar"></div>
</div>
<script type="text/javascript">
    jQuery("#sectionGird").jqGrid({
        url : 'section/queryAsList',
        datatype : "json",
        autowidth : true,
        rownumbers : true,
        height : 'auto',
        colNames : [
          '栏目ID',
          '栏目名称',
          '栏目描述',
          '创建时间',
          '栏目类型',
          '用户姓名',
          '用户ID',
          '列位置数',
          '行位置数',
          '是否激活'
        ],
        colModel : [ 
        {
          name : 'sectionid',
          index : 'sectionid',
          width : 60,
          key : true,
          hidden : true,
          align : 'center'
        },
        {
          name : 'sectionname',
          index : 'sectionname'
        },
        {
          name : 'sectiondesc',
          index : 'sectiondesc'
        },
        {
          name : 'createtime',
          index : 'createtime'
        },
        {
          name : 'sectiontype',
          hidden : true,
          index : 'sectiontype'
        },
        {
          name : 'username',
          index : 'username'
        },
        {
          name : 'userid',
          hidden : true,
          index : 'userid'
        },
        {
          name : 'colnumber',
          hidden : true,
          index : 'colnumber'
        },
        {
          name : 'rownumber',
          hidden : true,
          index : 'rownumber'
        },
        {
          name : 'status',
          index : 'status'
        }
        ],
        rowNum : 15,
        rowList : [ 15, 20, 25, 30 ],
        pager : '#sectionToolbar',
        // sortname : 'username',
        recordpos : 'right',
        viewrecords : true,
        sortorder : "desc",
        jsonReader : {
            repeatitems : false
        },
        multiselect : true,
        shrinkToFit : false,
        caption : "栏目表列表"
    });
    jQuery("#sectionGird").jqGrid('navGrid', '#sectionToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>
